//
//  IWStatus.m
//  ItcastWeibo
//
//  Created by apple on 14-5-8.
//  Copyright (c) 2014年 itcast. All rights reserved.
//

#import "Status.h"
#import "NSDate+Extension.h"
#import "MJExtension.h"
#import "Photo.h"
#import "PicInfo.h"
#import "TextType.h"

@implementation Status
MJCodingImplementation

+ (NSDictionary *)mj_objectClassInArray{
    return @{@"pic_urls" : @"Photo", @"url_struct" : @"Urlstruct", @"topic_struct" : @"Topic"};
}

- (NSString *)source{
    int loc = (int)[_source rangeOfString:@">"].location+1;
    if (loc>0) {
        int length = (int)[_source rangeOfString:@"</"].location - loc;
        _source = [_source substringWithRange:NSMakeRange(loc, length)];
        _source = [NSString stringWithFormat:@"来自 %@", _source];
    }
    return _source;
}

- (NSArray *)pics{
    if (_pics == nil) {
        NSMutableArray *pics = [NSMutableArray array];
        for (NSString *picID in _pic_ids) {
            PicInfo *pic = [PicInfo picInfo:(NSDictionary *)_pic_infos[picID]];
            [pics addObject:pic];
        }
        _pics = pics;
    }
    return _pics;
}


-(NSString *)created_at2{
    //获取微博的发送时间
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    fmt.dateFormat=@"EEE MMM dd HH:mm:ss Z yyyy";
    fmt.locale=[[NSLocale alloc]initWithLocaleIdentifier:@"en_US"];
    NSDate *createDate=[fmt dateFromString:_created_at];
    if (createDate) {
        //判断距离现在的时间
        if (createDate.isToday) {//是否是今天
            if (createDate.deltaWithNow.hour >= 1) {
                return [NSString stringWithFormat:@"%ld小时前", (long)createDate.deltaWithNow.hour];
            } else if (createDate.deltaWithNow.minute >= 1) {
                return [NSString stringWithFormat:@"%ld分钟前", (long)createDate.deltaWithNow.minute];
            } else {
                return @"刚刚";
            }
        }else if(createDate.isYesterday){//是否时昨天
            fmt.dateFormat = @"昨天 HH:mm";
            return [fmt stringFromDate:createDate];
        }else if(createDate.isThisYear){//是否是今年
            fmt.dateFormat = @"MM-dd HH:mm";
            return [fmt stringFromDate:createDate];
        }else{
            fmt.dateFormat = @"yyyy-MM-dd HH:mm";
            return [fmt stringFromDate:createDate];
        }
    }else{
        return _created_at2;
    }
   
}

@end
